/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package diskfilesystem;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Document extends File {   //继承File类进行扩展

    public char[] name = new char[4];        //目录名或文件名：3 个字节
    public char extendName;      //扩展名：1 个字节（可执行文件扩展名为 e，目录没有扩展名）
    public char attribute;       //属性为文件或目录,1表文件，0表目录
    public char begin;           //起始盘块号
    public char size;            //文件大小

    public Document() {
        super("new");
    }

    public Document(File f) {
        super(f.getName());
    }

    public Document(String name) {
        super(name);

    }

    @Override
    public Document[] listFiles() {

        File f[] = super.listFiles();
        Document[] d = new Document[f.length];
        for (int i = 0; i < f.length; i++) {
            d[i] = new Document(f[i]);
        }
        return d;
    }

    public Document rename(Document f, String newName) {
        int l = newName.length();
        if (newName.indexOf(".") != -1) {
            l -= 2;
        }
        Document newFile = new Document(newName);
        for (int i = 0; i < l; i++) {
            newFile.name[i] = newName.charAt(i);
        }
        newFile.attribute = f.attribute;
        newFile.extendName = f.extendName;
        newFile.begin = f.begin;
        newFile.size = f.size;
        return newFile;
    }

    public String read() {          //读，打开
        byte[] b = new byte[(int) this.length()];
        try {
            BufferedInputStream input = new BufferedInputStream(new FileInputStream(this));
            //System.out.print(this.length());
            int r;
            while ((r = input.read(b)) != -1);
            input.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Document.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Document.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new String(b);
    }

    public void write(String s) {       //写，保存
        try {
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(this));
            output.write(s.getBytes());
            output.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Document.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Document.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */
    public void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时 
                InputStream inStream = new FileInputStream(oldPath); //读入原文件 
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小 
                    //System.out.println(bytesum); 
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();

        }

    }

    /**
     * 复制整个文件夹内容
     *
     * @param oldPath String 原文件路径 如：c:/fqf
     * @param newPath String 复制后路径 如：f:/fqf/ff
     * @return boolean
     */
    public void copyFolder(String oldPath, String newPath) {

        try {
            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/"
                            + (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ((len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {//如果是子文件夹 
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
        } catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();

        }

    }

    public char getExtendName() {
        return this.extendName;
    }

    public char getAttribute() {
        return this.attribute;
    }

    public char getBegin() {
        return this.begin;
    }

    public char getSize() {
        return this.size;
    }

}
